package com.kingyun.gpsinspection.purificationservice.services.jsm.innerservice.impl.datereport;

import com.alibaba.fastjson.JSONObject;
import com.cqhilink.api.common.utils.UniqueUtil;
import com.cqhilink.api.common.utils.date.DateStyle;
import com.cqhilink.api.common.utils.date.DateUtil;
import com.kingyun.gpsinspection.purificationservice.dao.mapper.jsm.RepItemPTypeMapper;
import com.kingyun.gpsinspection.purificationservice.facade.model.jsm.Employee;
import com.kingyun.gpsinspection.purificationservice.facade.model.jsm.RepItemPType;
import com.kingyun.gpsinspection.purificationservice.facade.service.jsm.datareport.JsmRepItemPTypeService;
import com.kingyun.gpsinspection.purificationservice.facade.service.jsm.employee.JsmEmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * Created by huangxueqian on 2017/2/15/015.
 * 报表类型：位置类型的服务实现类
 */

public class JsmRepItemPTypeServiceImpl implements JsmRepItemPTypeService {
    @Autowired
    private RepItemPTypeMapper repItemPTypeMapper ;
    @Autowired
    private JsmEmployeeService employeeService ;

    /**
     * 列表注释问题
     * @param repItemPType
     * @return 列表注释问题：分页table
     * @throws Exception
     */
    @Override
    public JSONObject table(RepItemPType repItemPType) throws Exception {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("total",repItemPTypeMapper.queryRepItemPTypeCount(repItemPType));
        jsonObject.put("rows",repItemPTypeMapper.queryRepItemPTypeList(repItemPType));
        return jsonObject;
    }

    /**
     *
     * @param repItemPType
     * @return
     * @throws Exception
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public Integer add(RepItemPType repItemPType,Employee employee) throws Exception {
        Integer resInt = 0 ;
        if(repItemPType.getPositionId()==null){
            repItemPType.setPositionId(UniqueUtil.uuid());
            repItemPType.setCreator(employee.getEmployeeId());
            repItemPType.setModifyer(employee.getEmployeeId());
            repItemPType.setCreatetime(DateUtil.DateToString(new Date(), DateStyle.YYYY_MM_DD_HH_MM_SS));
            repItemPType.setModifytime(DateUtil.DateToString(new Date(), DateStyle.YYYY_MM_DD_HH_MM_SS));
            resInt = repItemPTypeMapper.insertSelective(repItemPType);
        }else{
            repItemPType.setModifyer(employee.getEmployeeId());
            repItemPType.setModifytime(DateUtil.DateToString(new Date(), DateStyle.YYYY_MM_DD_HH_MM_SS));
            resInt = repItemPTypeMapper.updateByPrimaryKeySelective(repItemPType);
        }
        return resInt;
    }

    /**
     * @param  positionIdList
     * @return
     * @throws Exception
     */

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public Integer del(List<String> positionIdList) throws Exception {
        Integer resInt = 0 ;
        if(positionIdList!=null){
            for(String postionId : positionIdList){
                resInt = resInt +   repItemPTypeMapper.deleteByPrimaryKey(postionId);
            }
        }
        return resInt ;
    }
}
